package com.vhr.service.impl;

import com.vhr.VO.OvertimeAdminVo;
import com.vhr.VO.OvertimeVO;
import com.vhr.entities.Employee;
import com.vhr.entities.Overtime;
import com.vhr.entities.OvertimeExample;
import com.vhr.mapper.OvertimeMapper;
import com.vhr.service.IOvertimeService;
import com.vhr.util.MonthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class IOvertimeServiceImpl implements IOvertimeService{

    @Autowired
    private OvertimeMapper overtimeMapper;
    /*根据员工外键查询员工加班信息*/
    @Override
    public List<Overtime> selectByEmployee(Employee emp) {
        OvertimeExample overtimeExample = new OvertimeExample();
        overtimeExample.createCriteria().andEmpIdEqualTo(emp.getEmpId());
        List<Overtime> overtimes = overtimeMapper.selectByExample(overtimeExample);
        return overtimes;
    }

//    根据员工外键和加班日期查找加班记录
    @Override
    public Overtime selectOvertimeByEmpIdAndOverTime(Integer empId, Date overTime) {
        OvertimeExample example = new OvertimeExample();
        OvertimeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdEqualTo(empId);
        criteria.andOvertimeTimeEqualTo(overTime);
        List<Overtime> list = overtimeMapper.selectByExample(example);
        if(list.size() == 0)
            return null;
        else
            return list.get(0);
    }

    //新增请假申请,加班当天时间
    @Transactional
    @Override
    public int addOvertime(Overtime overtime) {
       // log.info("【请假申请】：员工号："+overtime.getEmpId());
        //时间的月份，月份要减1
        overtime.setOvertimeTime(MonthUtil.MonthDecrease(overtime.getOvertimeTime()));
        System.out.println(overtime.getOvertimeTime());
        return overtimeMapper.insertSelective(overtime);
    }
    //审批请假，传入审批的请假信息和管理员信息
    @Transactional
    @Override
    public int judegOvertime(Overtime overtime,Employee admin,Integer status) {
        overtime.setOvertimeStatus(status);
        overtime.setJudgeId(admin.getEmpId());
      //  log.info("【请假审批】 管理员"+admin.getEmpId()+"  审批请假编号  "+overtime.getOvertimeId());
        int result = overtimeMapper.updateByPrimaryKeySelective(overtime);
        return result;
    }

    @Override
    public int updateByPrimary(Overtime overtime) {
        overtime.setOvertimeTime(MonthUtil.MonthDecrease(overtime.getOvertimeTime()));
        return overtimeMapper.updateByPrimaryKeySelective(overtime);
    }

    @Override
    public int deleteByPrimary(Overtime overtime) {
        return overtimeMapper.deleteByPrimaryKey(overtime.getOvertimeId());
    }

    //    员工查看自己的请假记录
    @Override
    public List<OvertimeAdminVo> selectPersonOvertime(int empId) {
       return overtimeMapper.selectById(empId);
    }

//    员工条件查看自己的加班记录
    @Override
    public List<Overtime> selectPersonOvertimeByStatus(int empId, int status) {
        OvertimeExample overtimeExample = new OvertimeExample();
        OvertimeExample.Criteria criteria = overtimeExample.createCriteria();
        criteria.andEmpIdEqualTo(empId);
        criteria.andOvertimeStatusEqualTo(status);
        return overtimeMapper.selectByExample(overtimeExample);
    }

    @Override
    public List<OvertimeAdminVo> selectByDeptAndStatusAndName(Integer deptId, Integer status, String empName) {
        return overtimeMapper.selectByDeptAndStatusAndName(deptId,status,empName);
    }

    //查询所有的请假信息
    @Override
    public List<OvertimeAdminVo> selectAll() {
        return overtimeMapper.selectAll();
    }

    //查询已经审批过的加班申请，1，2未处理过的加班申请，1为已通过，2为拒绝
    @Override
    public List<OvertimeAdminVo> selectOperated() {
        return overtimeMapper.selectOperated();
    }

    //查询未经审批的加班申请，0为未经审批的加班申请
    @Override
    public List<OvertimeAdminVo> selectNoOperated() {
        return overtimeMapper.selectNoOperated();
    }

}

